#include "stdio.h"
#include "mpool.h"

MPool<int> q(100);
int k=1;
void* rfun(void* arg){
    int k;
    fprintf(stdout, "get size [%d]\n", q.length());
    while((k=q.get(-1))!=-1){
        fprintf(stdout, "get [%d] size[%d]\n", k, q.length());
        fflush(stdout);
    }
}
void* wfun(void* arg){
    fprintf(stdout, "put size [%d]\n", q.length());
    while(q.put(k++)){
        fprintf(stdout, "put[%d] size[%d]\n", k, q.length());
        fflush(stdout);
    }
}

int main(){
    printf("get[%d]\n", q.get(-1));
    for(int i=0;i<30;i++){
        if(!q.put(i)){
            fprintf(stderr, "put error[%d]\n", i);
        }
    }
    pthread_t t1, t2;
    assert(pthread_create(&t1, NULL, &rfun, NULL)==0);
    assert(pthread_create(&t2, NULL, &wfun, NULL)==0);
    sleep(10);
    printf("End\n");
    
    return 0;
}
